package com.zzl.scala

object Lesson03 {
  def main(args: Array[String]): Unit = {
     val  str1 ="hello world"
     val  str2 ="hello world"
     val  str3 ="hello world"
    println(str1.indexOf('o'))
    println(str1.equals(str2))
    //创建一个可变长度的字符串
    val strBuilder = new StringBuilder
    strBuilder.+('F')
    strBuilder.++=("I love")
    strBuilder++=("ZZL")
    strBuilder.append("P")
    strBuilder+=('P')
    println(strBuilder)


    //数组
    val arr = new Array[Int](10)
    arr(0)=100
    arr(1)=99
    for(index<-0 until arr.length){
      arr(index)=index*index
    }
    arr.foreach(println)

    /**
      * 如何创建一个二维数组

      */

    var arr2 = new Array[Array[Int]](3)
    for(i<-0 until arr2.length){
      arr2(i) =new Array[Int](3)
    }
    //二维数组已经创建完毕
    //往二维数组中添加值
    for(i<-0 until arr2.length;j<- 0 until arr2(i).length){
        arr2(i)(j) = i*j
    }

    for(i<-0 until arr2.length;j<- 0 until arr2(i).length){
      print(arr2(i)(j)+"\t")
    }

    val arr3= Array(1,2,3,4)
    val arr4= Array(2,3,4,5)
    val arrAll = Array.concat(arr3,arr4)
    arrAll.foreach(println)
    //fill方法会创建一个初始值为5的长度为1的数组
    var fillArr = Array.fill(5)(1)
    for(e<- fillArr){
      println(e)
    }

    var list = List(1,2,3,4)
    var fl = list.filter(_>3)
    fl.foreach(println)
    println(list.count(_>0))

    val list1 = List("hello q","heelo","llo","cccasd")
    list1.foreach(println)
    println("================Map===================")
    val map = Map(
      "1"->"adad",
      2->"asdasasda",
      (3,"adasdsad")
    )
    val keyIterator = map.keys.iterator
    while(keyIterator.hasNext){
      var k =keyIterator.next()
      println(k+"======="+map.get(k).get)
    }
   println( map.get("10000").getOrElse("no "))

    println("=========元组===========")
    var t2 =(1,2)
    println(t2._1)
    println(t2._2)
    val t3 = Tuple3(1,2,4)
    val t4 = Tuple4("sdas",1,true,1.0)
    val t33 = (1,2,323.12,123)
    val iterable = t33.productIterator
    while(iterable.hasNext){
      println(iterable.next())
    }
    //二元组swap
    val s2 = t2.swap
    println(s2._1)

    //==================trait=============================
    /**
      * trait可以类比成java中的接口 但是比java中的接口高级一下
      * trait中可以定义方法也可以实现方法
      */
    val st = new Student
    st.read("asdads")
    st.listen("sda")
    st.sayName("adsdasas")
  }
}


trait Read{
  val readType = "Read"
  def read(name:String): Unit ={
    println(name+"is reading")
  }
  def sayName(name:String):String
}

trait Listen{
  def listen(name:String): Unit ={
    println(name+"is listing")
  }
}

class Student extends Read with Listen {
  override def sayName(name: String): String = {
    println(name)
    name
  }
}